# @Time : 2021/8/3 14:33
# @Author : Li Kunlun
# @Description : NDArray是一个类，用来存储和变换数据

from mxnet import nd
import numpy as np

x = nd.arange(12)
# [  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.]
# <NDArray 12 @cpu(0)>
print(x)

# 通过shape属性来获取NDArray实例的形状
# (12,)
print(x.shape)

# 12
print(x.size)

# [[  0.   1.   2.   3.]
#  [  4.   5.   6.   7.]
#  [  8.   9.  10.  11.]]
# <NDArray 3x4 @cpu(0)>
print(x.reshape((3, 4)))

# 创建一个各元素为0，形状为(2, 3, 4)的张量
# [[[ 0.  0.  0.  0.]
#   [ 0.  0.  0.  0.]
#   [ 0.  0.  0.  0.]]
#
#  [[ 0.  0.  0.  0.]
#   [ 0.  0.  0.  0.]
#   [ 0.  0.  0.  0.]]]
# <NDArray 2x3x4 @cpu(0)>
print(nd.zeros((2, 3, 4)))

# 通过Python的列表（list）指定需要创建的NDArray中每个元素的值
Y = nd.array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
print(Y)

# 对元素做指数运算
print(Y.exp())

# 矩阵乘法运算
X = x.reshape((3, 4))
print(nd.dot(X, Y.T))

# X.norm() 求解 X中的L2范数
# [ 22.49444389]
# <NDArray 1 @cpu(0)>
# 22.4944
print(X.norm())
print(X.norm().asscalar())

print("---------------------------NDArray和NumPy相互变换-------------------------")
P = np.ones((2, 3))
D = nd.array(P)

# Numpy -》 NDArray:
# [[ 1.  1.  1.]
#  [ 1.  1.  1.]]
# <NDArray 2x3 @cpu(0)>
print("Numpy -》 NDArray:", D)

# 将NDArray实例变换成NumPy实例
print(D.asnumpy())
